class Solution
{
public:
    string originalDigits(string s)
    {
        unordered_map<char, int> charCount;
        for (char c : s)
        {
            ++charCount[c];
        }

        string result;
        // (z)ero one t(w)o three four five si(x) seven ei(g)ht nine
        while (charCount.count('w') && charCount['w'] > 0)
        {
            charCount['t'] -= 1;
            charCount['w'] -= 1;
            charCount['o'] -= 1;
            result.push_back('2');
        }
        while (charCount.count('z') && charCount['z'] > 0)
        {
            charCount['z'] -= 1;
            charCount['e'] -= 1;
            charCount['r'] -= 1;
            charCount['o'] -= 1;
            result.push_back('0');
        }
        while (charCount.count('g') && charCount['g'] > 0)
        {
            charCount['e'] -= 1;
            charCount['i'] -= 1;
            charCount['g'] -= 1;
            charCount['h'] -= 1;
            charCount['t'] -= 1;
            result.push_back('8');
        }
        while (charCount.count('x') && charCount['x'] > 0)
        {
            charCount['s'] -= 1;
            charCount['i'] -= 1;
            charCount['x'] -= 1;
            result.push_back('6');
        }

        // one t(h)ree four five (s)even nine
        while (charCount.count('h') && charCount['h'] > 0)
        {
            charCount['t'] -= 1;
            charCount['h'] -= 1;
            charCount['r'] -= 1;
            charCount['e'] -= 1;
            charCount['e'] -= 1;
            result.push_back('3');
        }
        while (charCount.count('s') && charCount['s'] > 0)
        {
            charCount['s'] -= 1;
            charCount['e'] -= 2;
            charCount['v'] -= 1;
            charCount['n'] -= 1;
            result.push_back('7');
        }

        // one fou(r) fi(v)e n(i)ne
        while (charCount.count('v') && charCount['v'] > 0)
        {
            charCount['f'] -= 1;
            charCount['i'] -= 1;
            charCount['v'] -= 1;
            charCount['e'] -= 1;
            result.push_back('5');
        }
        while (charCount.count('i') && charCount['i'] > 0)
        {
            charCount['n'] -= 2;
            charCount['i'] -= 1;
            charCount['e'] -= 1;
            result.push_back('9');
        }
        while (charCount.count('r') && charCount['r'] > 0)
        {
            charCount['f'] -= 1;
            charCount['o'] -= 1;
            charCount['u'] -= 1;
            charCount['r'] -= 1;
            result.push_back('4');
        }

        // one
        while (charCount.count('o') && charCount['o'] > 0)
        {
            charCount['o'] -= 1;
            result.push_back('1');
        }

        sort(result.begin(), result.end());
        return result;
    }
};